package com.gzc.just.play.last.war.robotclient.ai;

import com.gzc.just.play.last.war.robotclient.RobotAI;
import com.gzc.just.play.last.war.robotclient.RobotPlayer;

import java.util.Random;

/**
 * Attack AI
 * Corresponds to C++ version CPlayerAtkAI
 */
public class AttackAI extends RobotAI {
    private final Random random = new Random();
    
    // Attack related parameters
    private long lastAttackTime = 0;
    private static final long ATTACK_INTERVAL = 2000; // 2 seconds attack interval
    
    public AttackAI(RobotPlayer player) {
        super(player);
    }
    
    @Override
    public int begin() {
        logInfo("Attack AI started");
        return 0;
    }
    
    @Override
    public int update(long currentTime) {
        if (getAIState() == AIState.RUNNING) {
            // Check if it's time to attack
            if (currentTime - lastAttackTime > ATTACK_INTERVAL) {
                // Find a target
                long targetId = getPlayer().findNearestEnemy();
                if (targetId != -1) {
                    // Perform attack
                    performAttack(targetId);
                    lastAttackTime = currentTime;
                    
                    // Return AI continue running
                    setAIState(AIState.RUNNING);
                    return 1;
                }
            }
        }
        
        return 0;
    }
    
    @Override
    public int end() {
        logInfo("Attack AI ended");
        setAIState(AIState.CLEARED);
        return 0;
    }
    
    /**
     * Perform attack
     */
    private void performAttack(long targetId) {
        logInfo("Robot {} attacking target {}", getPlayer().getRobotId(), targetId);
        
        // Simple attack implementation
        // In a real implementation, this would send attack message to server
        getPlayer().updateHP(10); // Reduce target HP by 10
    }
}